package com.nnfe.fyd.feature.login;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.nnfe.fyd.R;
import com.nnfe.fyd.ShareKey;
import com.nnfe.fyd.base.ActionBarFragment;
import com.nnfe.fyd.base.WebViewActivity;
import com.nnfe.fyd.bean.HttpResult;
import com.nnfe.fyd.bean.registerAndLogin.RegisterBean;
import com.nnfe.fyd.http.CallbackImpl;
import com.nnfe.fyd.http.Url;
import com.nnfe.fyd.utils.CountDownTimerUtils;
import com.nnfe.fyd.utils.DialogUtil;
import com.nnfe.fyd.utils.SharePrefUtil;
import com.nnfe.fyd.utils.StringUtil;
import com.nnfe.fyd.utils.ToastUtil;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Response;

/**
 * ----------------------------------
 * Created by xyd on 2017/3/16.
 * <p/>
 * 描  述：
 * <p/>
 * 修订历史:
 * ----------------------------------
 */

public class RegisterFragment extends ActionBarFragment {
    @Bind(R.id.et_user_name)
    EditText etUserName;
    @Bind(R.id.et_phone_number)
    EditText etPhoneNumber;
    @Bind(R.id.et_captcha)
    EditText etCaptcha;
    @Bind(R.id.tv_bt_capthca)
    TextView tvBtCapthca;
    @Bind(R.id.et_psd)
    EditText etPsd;
    @Bind(R.id.et_psd_again)
    EditText etPsdAgain;
    @Bind(R.id.cb_agree)
    CheckBox cbAgree;
    @Bind(R.id.tv_register_protocol)
    TextView tvRegisterProtocol;
    private String userName;
    private String phoneNum;
    private String captcha;
    private String password;

    @Override
    public Object getTitleString() {
        return "注册";
    }

    @Override
    public int getContentView() {
        return R.layout.fragment_register;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        initView();
        return rootView;
    }

    @Override
    protected void initView() {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.tv_bt_capthca, R.id.tv_register_protocol, R.id.btn_next})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_register_protocol:
                WebViewActivity.lauch(mContext, "用户注册协议", Url.URL_register);
                break;
            case R.id.tv_bt_capthca:
                if (checkCanSendCaptcha()) {
                    sendCaptcha();
                }
                break;
            case R.id.btn_next:
                if (checkCanRegister()) {
                    regist();
                }
                break;
        }
    }

    private void sendCaptcha() {
        Call<HttpResult<String>> call = service.sendCaptcha(phoneNum);
        call.enqueue(new CallbackImpl<String>() {
            @Override
            public void responseSuccess(Call<HttpResult<String>> call, Response<HttpResult<String>> response) {
                if (response.body().getStatus() == 0) {
                    ToastUtil.showToast("验证码发送成功！");
                    CountDownTimerUtils countDownTimerUtils = new CountDownTimerUtils(tvBtCapthca, 60000, 1000);
                    countDownTimerUtils.start();
                }
            }
        });
    }

    /**
     * 验证是否符合发送验证码条件
     *
     * @return
     */
    private boolean checkCanSendCaptcha() {
        phoneNum = etPhoneNumber.getText().toString().trim();
        if (!StringUtil.isMobile(phoneNum)) {
            ToastUtil.showToast("请输入正确的手机号");
            return false;
        }

        return true;
    }

    private void regist() {
        DialogUtil.showLoadingDialog(mContext, "注册中");
        Call<HttpResult<RegisterBean>> call = service.doRegister(captcha, userName, phoneNum, password, "APP");
        call.enqueue(new CallbackImpl<RegisterBean>() {
            @Override
            public void responseSuccess(Call<HttpResult<RegisterBean>> call, Response<HttpResult<RegisterBean>> response) {

                if (response.body().getStatus() == 0) {
                    ToastUtil.showToast("注册成功");
                    saveData(response.body().getData());
                    mContext.onBackPressed();
                }
            }

            @Override
            protected void onRequestFinish() {
                DialogUtil.dismissLoadingDialog(mContext);
            }
        });
    }

    /**
     * 保存个人信息
     *
     * @param data
     */
    private void saveData(RegisterBean data) {

        SharePrefUtil.saveString(mContext, ShareKey.Mobile, data.mobile);
        SharePrefUtil.saveString(mContext, ShareKey.Name, data.name);

    }

    /**
     * 验证是否符合注册的条件
     *
     * @return
     */
    private boolean checkCanRegister() {
        userName = etUserName.getText().toString().trim();
        phoneNum = etPhoneNumber.getText().toString().trim();
        captcha = etCaptcha.getText().toString().trim();
        password = etPsd.getText().toString().trim();
        String passwordAgain = etPsdAgain.getText().toString().trim();

        if (TextUtils.isEmpty(userName)) {
            ToastUtil.showToast("请输入姓名!");
            return false;
        }

        if (!StringUtil.isMobile(phoneNum)) {
            ToastUtil.showToast("请输入正确的手机号码!");
            return false;
        }

        if (TextUtils.isEmpty(captcha)) {
            ToastUtil.showToast("请输入验证码!");
            return false;
        }

        if (TextUtils.isEmpty(password)) {
            ToastUtil.showToast("密码不能为空");
            return false;
        }

        if (password.length()<8){
            ToastUtil.showToast("密码长度不能少于8位！");
            return false;
        }

        if (TextUtils.isEmpty(passwordAgain)) {
            ToastUtil.showToast("确认密码不能为空");
            return false;
        }


        if (!password.equals(passwordAgain)) {
            ToastUtil.showToast("两次密码输入不一致！");
            return false;
        }


        if (!cbAgree.isChecked()) {
            ToastUtil.showToast("请同意用户注册协议！");
            return false;
        }

        return true;
    }

}
